/**
 * @filename		AlertUtil.java
 * @author			dhy
 * @date			2016年7月05日-下午3:27:09
 * @description	TODO
 */
package com.android.functionmanagement.util;

import android.content.Context;
import android.media.AudioManager;
import android.media.SoundPool;
import android.media.SoundPool.OnLoadCompleteListener;
import android.util.SparseIntArray;

/**
 * @author dhy
 * 
 */
public class AlertUtil implements OnLoadCompleteListener {
	private SoundPool soundPool;

	private SparseIntArray mHashSid = new SparseIntArray();

	private AlertUtil() {
		soundPool = new SoundPool(5, AudioManager.STREAM_ALARM, 0);
		soundPool.setOnLoadCompleteListener(this);
	}

	public void release() {
		if (this.soundPool == null)
			return;
		this.soundPool.release();
		mHashSid.clear();
		soundPool = null;
	}

	public int load(String path, int count, int priority) {
		mCount = count;
		mPriority = priority;
		soundPool.setVolume(AudioManager.STREAM_ALARM, 15);
		return soundPool.load(path, priority);
	}

	public int load(Context application, int rawid) {
		return load(application, rawid, 0, 1);
	}

	int mCount = 0;
	int mCurrentSid = 0;
	int mPriority = 1;

	/**
	 * 
	 * @param application
	 * @param rawid
	 * @param count
	 *            ,-1为循环模式 ,0单次，>0为,count次。
	 * @param priority
	 *            ,播放优先级。。
	 * @return soundId
	 */
	public int load(Context application, int rawid, int count, int priority) {
		mCount = count;
		mPriority = priority;
		soundPool.setVolume(AudioManager.STREAM_ALARM, 15);
		return soundPool.load(application, rawid, priority);
	}

	/**
	 * 
	 * @param rawid
	 */
	public void stop(int rawid) {
		soundPool.stop(mHashSid.get(rawid));
		mHashSid.delete(rawid);
	}

	private static class InstanceHolder {
		private static AlertUtil instance = new AlertUtil();
	}

	public static AlertUtil getInstance() {
		if (InstanceHolder.instance == null) {
			InstanceHolder.instance = new AlertUtil();
		}
		return InstanceHolder.instance;
	}

	@Override
	public void onLoadComplete(SoundPool soundPool, int sampleId, int status) {
		// TODO Auto-generated method stub
		mCurrentSid = soundPool.play(sampleId, 1, 1, mPriority, mCount, 1);
		mHashSid.put(sampleId, mCurrentSid);
		mCount = 0;
		mPriority = 1;
	}
}
